import {
    totalConfig,
    propName
} from 'server/config/templateConfig';
import MonsterModel from 'server/model/monsterModel';
import battleHandle from './battleHandle';
const mapListConfig = totalConfig.mapList;
import utils from './utils';
const mapConfig = totalConfig.map;
let itemConfig = totalConfig.item;

class MapHandle {
    constructor() {
        Object.keys(mapListConfig).forEach((mapListId) => {
            let item = mapListConfig[mapListId];
            let eventRateTotal = 0;
            if (item.explore) {
                item.explore.eventRate.forEach((i) => {
                    eventRateTotal += i;
                })
                item.explore.eventRateTotal = eventRateTotal;
            }
        });
    }

    createMap(player) {
        player.basicInfo.map = mapList.slice();
    }

    explore(player) {
        let mapId = player.basicInfo.mapId;
        let exploreInfo = mapListConfig[mapId].explore;
        let event = exploreInfo.event;
        let eventRate = exploreInfo.eventRate;
        let rateValue = utils.getRandom(0, exploreInfo.eventRateTotal);
        let data;
        let i = eventRate.length;
        while (i--) {
            i = 1;
            // if (rateValue <= eventRate[i]) {

            switch (i) { //0什么事情也没发生,1遇怪,2陷阱，沼泽,3发现物品,4遇到事件,5遇到npc,6区域boss
                case 0: //什么事情也没发生
                    data = {};
                    break;
                case 1: //遇怪
                    data = this.battle(player, event[i]);
                    break;
                case 2: //陷阱，沼泽
                    data = this.trouble(player, event[i]);
                    break;
                case 3: //发现物品
                    data = this.getItem(player, event[i]);
                    break;
                case 4: //遇到事件
                    data = this.meetEvent(player, event[i]);
                    break;
                case 5: //遇到npc
                    data = this.meetNpc(player, event[i]);
                    break;
                case 6: //区域boss
                    data = this.regionBoss(player, event[i]);
                    break;
            }
            data.type = i;
            return data;
            // } else {
            //     rateValue -= eventRate[i];
            // }
        }
    }

    battle(player, exploreInfo) { //遇怪
        let monsterId = utils.getOneFromArray(exploreInfo);
        let monster = new MonsterModel(monsterId);
        let playerFightAttri = player.fightAttriCopy();
        let monsterFightAttri = monster.fightAttriCopy();
        let result = battleHandle.attack(player, monster);
        let battleInfo = {};
        battleInfo.result = result;
        if (result === 1) { // 胜利
            let itemId = utils.getOneFromArray(monster.monsterInfo.items);
            let lvl = monster.fightAttri.lvl;
            battleInfo.item = [itemId, 1];
            battleInfo.exp = lvl;
            player.getExp(lvl);
            player.basicInfo.mapInfo = {
                mapType: 2,
                item: [itemId, 1]
            }
        }
        return {
            player: playerFightAttri,
            monster: monsterFightAttri,
            battleInfo: battleInfo
        }
    }

    trouble(player, exploreInfo) { //陷阱，沼泽
        let hurt = exploreInfo[0].hurt;
        player.fightAttri.hp -= hurt;
        return {
            player: player.fightAttri,
            hurt: hurt
        }
    }

    getItem(player, exploreInfo) { //获得物品
        let itemId = exploreInfo[0];
        player.basicInfo.mapInfo = {
            mapType: 3,
            item: [itemId, 1]
        }
        return {
            item: [itemId, 1]
        }
    }

    meetEvent(player, exploreInfo) { //遇到事件
        player.basicInfo.mapInfo = {
            mapType: 4,
            value: [1, 2]
        }
        return {
            eventId: exploreInfo[0]
        }
    }

    meetNpc(player, exploreInfo) { //遇到npc
        return {
            eventId: exploreInfo[0]
        }
    }

    regionBoss(player, exploreInfo) { //遇到区域boss
        // return {
        //     eventId: exploreInfo[0]
        // }
    }

    collection(player, mapInfo) { //采集
        let itemId = mapInfo.item[0];
        let item = [itemId, 1];
        player.bag.addItem(item) //添加物品
        return {
            itemId: itemId,
            num: 1
        }
    }

    mining(player, mapInfo) { //采矿
        let itemId = mapInfo.item[0];
        let item = [itemId, 1]
        player.bag.addItem(item); //添加物品
        return {
            itemId: itemId,
            num: 1
        }
    }

    attackFam(player, mapInfo) { //挑战副本
        let monsterId = mapInfo.monster[0];
        return {
            player: player.fightAttri,
            monster: new MonsterModel(monsterId).fightAttri
        }
    }
}

let mapHandle = new MapHandle();
export default mapHandle;